package com.example.springmvcdemo1;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/xmy")
@RestController
public class RequestController {
    @RequestMapping("/hello")
    public String helloSpringMVC(){
        return "hello Spring MVC";
    }
    //传单个参数
    @RequestMapping("/r1")
    public String r1(String name){
        return "接收到来自"+name+"的请求";
    }
    //对于参数为空，建议采用包装类型(可以尝试传String类型的参数，或者不传参数)
    @RequestMapping("/r2")
    public String r2(Integer age){
        return "接收到来自age="+age+"的请求";
    }
    @RequestMapping("/r3")
    public String r3(int age){
        return "接收到来自age="+age+"的请求";
    }
    //后端参数重命名(如果不传参r4会400，r5不会)
    //1、参数重命名
    //2、参数必传
    @RequestMapping("/r4")
    public String r4(@RequestParam("name")String userName){
        return userName;
    }
    @RequestMapping("/r5")
    public String r5(@RequestParam(value = "name",required = false)String userName){
        return userName;
    }
    //传多个参数
    @RequestMapping("/r6")
    public String r6(String name,Integer age){
        return "接收到来自 "+name+" age = "+age+" 的请求";
    }
    //传入对象
    @RequestMapping("/r7")
    public String r7(Student student){
        return student.toString();
    }
}
